
use "data files/sen_r2_local.dta", clear

gen year_survey = 2002

gen round = 2

***********************
***   Demographic   ***
***********************

decode milieu, gen(urbrur)
replace urbrur = "Urban" if urbrur == "Urbain"

gen age = q80
label var age "Age of respondent"
replace age=. if age==998 | age==999
drop q80

decode q96, gen(gender)
drop q96

gen no_school = 0 if q84!=-1 & q84!=. & q84!=99
replace no_school = 1 if q84==0 
label var no_school "No formal schooling"
gen informal_school = 0 if q84!=-1 & q84!=. & q84!=99
replace informal_school = 1 if q84==1 & q84<=9
label var informal_school "Informal schooling only"

gen primary_school_inc = 0 if q84!=. & q84!=98 & q84!=99
replace primary_school_inc = 1 if q84>=2 & q84<=9
gen primary_school = 0 if q84!=. & q84!=98 & q84!=99
replace primary_school = 1 if q84>=3 & q84<=9
gen secondary_school_inc = 0 if q84!=. & q84!=98 & q84!=99
replace secondary_school_inc = 1 if q84>=4 & q84<=9
gen secondary_school = 0 if q84!=. & q84!=98 & q84!=99
replace secondary_school = 1 if q84>=5 & q84<=9
gen college=  0 if q84!=. & q84!=98 & q84!=99
replace college = 1 if q84>=7 & q84<=9
label var college "Some college, even if incomplete"
drop q84

gen religion=""
replace religion="None" if q85==0 | q85==8 | q85==9
replace religion="Muslim" if q85==1
replace religion="Christian" if q85==2 | q85==3 | q85==4 | q85==5 | q85==10
replace religion="Traditional" if q85==6
replace religion="Other" if q85==7  | q85==11 | q85==12  | q85==260 | q85==995
replace religion="Unknown" if q85==998  | q85==-1 | q85==999
tab q85 if religion==""


gen ethnicity = q96asen
replace ethnicity = . if q96asen < 0

gen wolof = 0 if ethnicity != .
replace wolof = 1 if ethnicity == 220

gen toucpeul = 0 if ethnicity != .
replace toucpeul = 1 if ethnicity == 323

gen serer = 0 if ethnicity != .
replace serer = 1 if ethnicity == 221

gen bambara = 0 if ethnicity != .
replace bambara = 1 if ethnicity == 222

gen soninke = 0 if ethnicity != .
replace soninke = 1 if ethnicity == 225

gen diola = 0 if ethnicity != .
replace diola = 1 if ethnicity == 224

gen others = 0 if ethnicity != .
replace others = 1 if ethnicity == 225 | ethnicity == 995


gen language = q97

gen french_lang = 0 if language != .
replace french_lang = 1 if language == 2

gen wolof_lang = 0 if language != .
replace wolof_lang = 1 if language == 220

gen toucpeul_lang = 0 if language != .
replace toucpeul_lang = 1 if language == 223

gen serer_lang = 0 if language != .
replace serer_lang = 1 if language == 221

gen mand_lang = 0 if language != .
replace mand_lang = 1 if language == 222 

gen diola_lang = 0 if language != .
replace diola_lang = 1 if language == 224

gen other_lang = 0 if lang != .
replace other_lang = 1 if wolof_lang == 0 & toucpeul_lang == 0 & serer_lang == 0 & mand_lang == 0 & diola_lang == 0


********************
***   Economic   ***
********************

gen no_food = 0 if q9a!=-1 & q9a!=. & q9a!=9
replace no_food = 1 if q9a==2 | q9a==3 | q9a==4

gen no_water = 0 if q9b!=-1 & q9b!=. & q9b!=9
replace no_water = 1 if q9b==2 | q9b==3 | q9b==4

gen no_healthcare = 0 if q9c!=-1 & q9c!=. & q9c!=9
replace no_healthcare = 1 if q9c==2 | q9c==3 | q9c==4

gen no_fuel = 0 if q9e!=-1 & q9e!=. & q9e!=9
replace no_fuel = 1 if q9e==2 | q9e==3 | q9e==4

gen no_income = 0 if q9f!=-1 & q9f!=. & q9f!=9
replace no_income = 1 if q9f==2 | q9f==3 | q9f==4
drop q9a - q9f

**** Employment variables ***
gen employment_status = "" if q89!=. & q89!=-1 & q89!=9
replace employment_status = "inactive" if  q89==0
replace employment_status = "unemployed" if q89==1
replace employment_status = "part-time" if q89==2 | q89==3
replace employment_status = "full-time" if q89==4 | q89==5
drop q88 q89 q90

*** Assets *** 
* Equivalent in Senegal r2 not found


**************************
***   Interest index   ***
**************************


gen discuss_pol_index = q25a if q25a >= 0 & q25a <= 4
recode discuss_pol_index (0/1=0) (2/3=1) (4=2) 

gen interest_pub_affairs_index = q27 if q27!=-1 & q27!=. & q27!=9

gen radio = q26a if q26a!=-1 & q26a!=. & q26a!=9 & q26a != 98
gen tv = q26b if q26b!=-1 & q26b!=. & q26b!=9 & q26b != 98
gen newspaper = q26c if q26c!=-1 & q26c!=. & q26c!=9 & q26c != 98

gen radio_news_often =0 if q26a!=-1 & q26a!=. & q26a!=9
replace  radio_news_often = 1 if q26a==4

gen tv_news_often =0 if q26b!=-1 & q26b!=. & q26b!=9
replace  tv_news_often =1 if q26b==4

gen newspaper_news_often =0 if q26c !=-1 & q26c !=. & q26c !=9
replace  newspaper_news_often =1 if q26c ==4


*************************
***   Participation   ***
*************************

gen contact_LG_councilor_index = q29a if q29a!=. & q29a!=-1 & q29a!=9 & q29a != 98

gen contact_MP_index = q29b if q29b!=. & q29b!=-1 & q29b!=9 & q29b != 98

gen contact_officials_index = q29c if q29c!=. & q29c!=-1 & q29c!=9 & q29c != 98

gen att_demonstration_index = q25d if q25d!=. & q25d!=-1 & q25d!=9 & q25d != 98

gen raise_issue_index = q25c  if q25c!=. & q25c!=-1 & q25c!=9 & q25c != 98

gen vote1 = 0 if q87dsene !=-1 & q87dsene !=9 &  q87dsene !=.
replace vote1 = 1 if q87dsene<=220 & q87dsene>=224 | q87dsene==995

gen close_pds = 0 if q87a !=-1 & q87a !=.
replace close_pds = 1 if q87a == 220


gen att_rally = .
gen att_pol_meeting = .
gen persuade_others = .
gen work_for_cand = .



***********************************
***   Democratic institutions   ***
***********************************

*NEEDS TO BE CHECKED /

gen trust_EC=q43c if q43c!=. & q43c!=-1 &  q43c!=9

gen trust_courts=q43j if q43j !=. & q43j !=-1 & q43j!=9

gen careful_say = q41a if q41a !=. & q41a!= -1 & q41a!=9 

gen how_democratic=0 if q37!=.  & q37!=-1  & q37!=9
replace how_democratic=1 if q37==3
replace how_democratic=2 if q37==4
label var how_democratic "Believe that Senegal is a functioning democracy"
drop q37 

gen satisfaction_democracy = 0 if q40!=. & q40!=-1 & q40!=9
replace satisfaction_democracy = 1 if q40==3
replace satisfaction_democracy = 2 if q40==4
label var satisfaction_democracy "Satisfaction democracy"
drop q40 

gen checks_balances =0 if q33!=. & q33!=-1 &  q33!=9
replace checks_balances =1 if q33==1 |  q33==2

gen checks_balances_strong =0 if q33!=. & q33!=-1 & q33!=9
replace checks_balances_strong =1 if q33==1
drop q33

gen term_limits =0 if q32!=.  & q32!=-1  & q32!=9 
replace term_limits  =1 if q32 == 3 | q32 == 4

gen term_limits_strongly =0 if q32!=.  & q32!=-1 & q32!=9 
replace term_limits_strongly  =1 if q32 ==4
drop q32

gen against_one_party_rule =0 if q35a!=. & q35a!=-1 & q35a!=9
replace against_one_party_rule =1 if q35a == 1 | q35a==2

gen a_one_party_rule_strong =0 if q35a!=. & q35a!=-1 & q35a!=9
replace a_one_party_rule_strong =1 if q35a == 1 

gen reject_military_rule =0 if q35c!=. & q35c!=-1 & q35c!=9
replace reject_military_rule =1 if q35c == 1 | q35c==2

gen r_mil_rule_str =0 if q35c!=. & q35c!=-1 & q35c!=9
replace r_mil_rule_str =1 if q35c == 1

gen reject_one_man_rule =0 if q35d!=. & q35d!=-1 &  q35d!=9
replace reject_one_man_rule =1 if q35d == 1 | q35d==2

gen r_one_man_r_strongly =0 if q35d!=. & q35d!=-1 & q35d!=9
replace r_one_man_r_strongly =1 if q35d == 1 
drop q35*

gen election_choice_leaders = 0 if q30!=.  & q30!=-1  & q30!=9
replace election_choice_leaders = 1 if q30==1  | q30==2

gen election_choice_leaders_str = 0 if q30!=.  & q30!=-1  & q30!=9
replace election_choice_leaders_str = 1 if q30==1 
drop q30

gen supports_democracy=0 if q38!=. & q38!=-1
replace supports_democracy=1 if q38==3
label var supports_democracy "Supports democracy"
drop q38


*** Trust in different institutions ***

gen trust_president = q43a if q43a!=. & q43a!=-1 & q43a!=9

gen trust_national_assembly= q43b if q43b!=. &  q43b!=-1 & q43b!=9

gen trust_LG_council= q43e if q43e!=. & q43e!=-1 & q43e!=9

gen trust_ruling_party= q43f if q43f!=. & q43f!=-1 & q43f!=9

gen trust_opposition_party= q43g if q43g!=. & q43g!=-1 & q43g!=9

drop q43*


*** Government performance ***

gen present_econ = q1a - 1 if q1a!=. & q1a!=-1 & q1a!=9
label var present_econ "Economic conditions"

gen personal_econ = q1b - 1 if q1b!=. & q1b!=-1 & q1b!=9
label var personal_econ "Personal economic conditions"

gen past_econ = q3a - 1 if q3a!=. & q3a!=-1 & q3a!=9
label var past_econ "Compare econ, past 12mths"

gen future_econ = q4a - 1 if q4a!=. & q4a!=-1 & q4a!=9
label var future_econ "Future econ (12mth)"

gen handles_economy = q45a-1 if q45a!=. & q45a!=-1 & q45a!=9
label var handles_economy "Government handles economy"

gen handles_employment= q45b-1  if q45b!=. & q45b!=-1 & q45b!=9
label var handles_employment "Government handles employment"

gen handles_inflation= q45c-1 if q45c!=. & q45c!=-1 & q45c!=9
label var handles_inflation "Government handles inflation"

gen handles_inequality = q45d-1 if q45d!=. & q45d!=-1 & q45d!=9
label var handles_inequality "Government handles inequality"

gen handles_health= q45f-1 if q45f!=. & q45f!=-1 & q45f!=9
label var handles_health "Government handles health provision"

gen handles_education= q45g-1 if q45g!=. & q45g!=-1 & q45g!=9
label var handles_education "Government handles education provision"

gen handles_water= q45h-1 if q45h!=. & q45h!=-1 & q45h!=9
label var handles_water "Government handles water provision"
drop q45*

* local gov performance variables not in r2

gen performance_president = q48a-1 if q48a!=. & q48a!=-1 & q48a!=9
label var performance_president "Approves President perfomance"
 
gen performance_mp  = q48b-1 if q48b!=. & q48b!=-1 & q48b!=9
label var performance_mp "Approves MP perfomance"

gen performance_LG_councilor  = q48d2sen-1 if q48d2sen!=. & q48d2sen!=-1 & q48d2sen!=9
label var performance_LG_councilor "Approves LG councilor perfomance"
drop q48*

gen leaders_listen = q50b if q50b !=. & q50b !=-1 & q50b !=9
label var  leaders_listen "Leaders listen to people"
drop q50b


gen corruption_president = q51a if q51a !=. &  q51a !=-1 &  q51a !=9
label var corruption_president "Very corrupt president office"

gen corruption_MP = q51b if q51b !=. &  q51b !=-1 &  q51b !=9
label var corruption_president "Very corrupt MPs"

gen corruption_government_officials = q51c if q51c  !=. &  q51c  !=-1 &  q51c  !=9
label var corruption_government_officials "Very corrupt LG councilors"

gen corr_pres_dk = q51a == 9  if q51a !=. &  q51a !=-1
gen corr_mp_dk = q51b == 9  if q51b !=. &  q51b !=-1
gen corr_gov_dk = q51c == 9  if q51c !=. &  q51c !=-1

drop q51*



ren numrep respno

drop numrpold-dheure
drop q1a-q115d
